/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.Impl;

import com.startup.musicstore.app.facade.Facade;
import com.startup.musicstore.app.factory.AppFactory;
import com.startup.musicstore.model.Album;
import com.startup.musicstore.model.Customer;
import com.startup.musicstore.model.Demographic;
import com.startup.musicstore.model.OrderItem;
import com.startup.musicstore.model.Orders;
import com.startup.musicstore.model.Review;
import com.startup.musicstore.model.Track;
import com.startup.musicstore.services.AlbumService;
import com.startup.musicstore.services.crud.AlbumCrudService;
import com.startup.musicstore.services.crud.CustomerCrudService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author aljay19
 */
@Service("albumService")
@Transactional
public class AlbumServiceImpl implements AlbumService {

    private Facade data = new Facade();
    @Autowired
    private AlbumCrudService albumCrudService;

    @Override
    public int findRating(Long id) {

        albumCrudService = data.getAlbumCrudService();

        Album album = albumCrudService.findById(id);
        Review review = album.getmarketingInformation().getReview();
        int rating = review.getRating();

        return rating;
    }

    @Override
    public String findTrackUrl(Track track) {

        String url = track.getSampleClip().getSampleUrl();

        return url;

    }

    @Override
    public List<Album> listAlbumsBoughtByGender(String gender) {

        CustomerCrudService customerCrudService = data.getCustomerCrudService();
        
        List<Customer> allCustomers = customerCrudService.findAll();
        List<Customer> customers = new ArrayList<Customer>();
        List<Album> albums = new ArrayList<Album>();
        
        for (Customer c : allCustomers) {
            Demographic demographic = c.getDemographic();
            if (gender.equals(demographic.getGender())) {
                customers.add(c);
            }
        }

        for (Customer c : customers) {
            List<Orders> orders = c.getOrders();
            for (Orders o : orders) {
                List<OrderItem> orderItems = o.getOrderItems();
                for (OrderItem oi : orderItems) {
                    albums.add(oi.getAlbum());
                }
            }
        }

        return albums;
    }
}
